package main

import (
	"Learn/base"
	"Learn/commit"
	"Learn/router"
	"os"

	"github.com/kataras/iris"
	"github.com/kataras/iris/middleware/logger"
	recover2 "github.com/kataras/iris/middleware/recover"
)

func main() {
	// 设置Env
	base.LoadEnv()

	//设置ID节点
	err := commit.SetIDNode()
	if err != nil {
		base.App.Logger().Error(err)
		return
	}

	app := iris.New()
	base.SetApplication(app)
	base.App.Logger().SetLevel(os.Getenv("APP_LOGGER_LEVEL"))

	// 设置数据库
	err = base.SetDatabase()
	if err != nil {
		base.App.Logger().Error(err)
		return
	}

	err = base.SetRedis()
	if err != nil {
		base.App.Logger().Error(err)
		return
	}

	base.App.Use(recover2.New())
	base.App.Use(logger.New())

	router.SetRouterApi()

	ip := os.Getenv("APP_HTTP_ADDRESS")
	port := os.Getenv("APP_HTTP_PORT")

	_ = app.Run(iris.Addr(ip+":"+port), iris.WithoutServerError(iris.ErrServerClosed))
}
